package com.tyqx.detectcputemperaturesendmessage;

import com.tyqx.detectcputemperaturesendmessage.config.SendPingRequestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class SchedulerNetworkInterruptionAndSendMessage {


    private static final Logger logger = LoggerFactory.getLogger(SchedulerGpuSendMessage.class);


    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private Constant constant;

    @Resource
    private SendPingRequestUtil sendPingRequestUtil;

   /* //ping网址1
    String pingWebsiteAddress1 = constant.ping_website1;

    //ping网址2
    String pingWebsiteAddress2 = constant.ping_website2;

    //ping网址3
    String pingWebsiteAddress3 = constant.ping_website3;*/

    //测试ip
    String testIpAddress = "203.205.137.11111";
    //System.out.println(sendPingRequest(tencentIpAddress1));

    Integer pingCount = 0;
    //表示每隔秒
    @Scheduled(fixedRate = 60 * 1000 * 1)
    //@Scheduled(fixedDelay = 1000 * 60 * 3)
    public void testTasks() throws Exception {
        logger.info("检测服务器网络是否中断定时器开始执行："+dateFormat.format(new Date())+"\n");
        //ping网址1
        String pingWebsiteAddress1 = constant.ping_website1;

        //ping网址2
        String pingWebsiteAddress2 = constant.ping_website2;

        //ping网址3
        String pingWebsiteAddress3 = constant.ping_website3;
        try{

            int pingTimes = 2;

            int timeout = 3000;

            boolean baiduIpFlag = sendPingRequestUtil.ping(pingWebsiteAddress1,pingTimes,timeout);

            boolean taoBaoIpFlag = sendPingRequestUtil.ping(pingWebsiteAddress2,pingTimes,timeout);

            boolean tencentIpFlag = sendPingRequestUtil.ping(pingWebsiteAddress3,pingTimes,timeout);
//            baiduIpFlag = false;
//            taoBaoIpFlag = false;
//            tencentIpFlag = false;

            if (baiduIpFlag || taoBaoIpFlag || tencentIpFlag) {
                pingCount = 0;
                String printText = "";
                if(baiduIpFlag){
                    printText = printText + " \t pingWebsiteAddress1 连接正常 \t";
                }else{
                    printText = printText + " \t pingWebsiteAddress1 连接异常 \t";
                }
                if(taoBaoIpFlag){
                    printText = printText + " \t pingWebsiteAddress2 连接正常 \t";
                }else{
                    printText = printText + " \t pingWebsiteAddress2 连接异常 \t";
                }
                if(tencentIpFlag){
                    printText = printText + " \t pingWebsiteAddress3 连接正常 \t";
                }else{
                    printText = printText + " \t pingWebsiteAddress3 连接异常 \t";
                }
                logger.info("服务器网络连接正常："+printText);
            }else{
                pingCount = pingCount + 1;
                logger.error("服务器网络连接中断:"+pingCount+"分钟");
            }

            if(pingCount>3){
                pingCount = 0;
                logger.error("服务器网络连接异常超过3分钟，关闭服务器");
                sendPingRequestUtil.shutdown();
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        logger.info("检测服务器网络是否中断定时器结束执行："+dateFormat.format(new Date())+"\n");
    }
}
